<?php

declare(strict_types=1);

namespace App\Api\Controller;

use App\Api\Middleware\ApiLockMiddleware;
use App\Api\Middleware\CheckAuthMiddleware;
use App\Api\Middleware\ResponseMiddleware;
use App\Api\Request\OrderRequest;
use App\Api\Service\OrderService;
use App\Model\Order;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;

#[Controller(prefix: 'api'), Middlewares([ResponseMiddleware::class])]
class OrderController
{
    /**
     * 订单确认
     * @param OrderRequest $request
     * @param OrderService $service
     * @return array
     */
    #[GetMapping('orders/confirm'), Middlewares([CheckAuthMiddleware::class])]
    public function confirm(OrderRequest $request, OrderService $service): array
    {
        return $service->confirm($request->validated());
    }

    /**
     * 订单创建
     * @param OrderRequest $request
     * @param OrderService $service
     * @return array
     */
    #[PostMapping('orders'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function store(OrderRequest $request, OrderService $service): array
    {
        return $service->store($request->validated());
    }

    /**
     * 获取支付参数
     * @param mixed $id
     * @param OrderService $service
     * @return array
     */
    #[GetMapping('orders/{id}/pay_parameter'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function payParameter(mixed $id, OrderService $service): array
    {
        return $service->payParameter($id);
    }

    /**
     * 订单支付成功(测试使用)
     * @param mixed $id
     * @param OrderService $service
     * @return void
     */
    #[GetMapping('orders/{id}/pay_success'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function paySuccess(mixed $id, OrderService $service): void
    {
        $service->paySuccess($id);
    }

    /**
     * 订单列表
     * @param OrderService $service
     * @return LengthAwarePaginatorInterface
     */
    #[GetMapping('orders'), Middlewares([CheckAuthMiddleware::class])]
    public function index(OrderService $service)
    {
        return $service->index();
    }

    /**
     * 订单详情
     * @param mixed $id
     * @param OrderService $service
     * @return Order
     */
    #[GetMapping('orders/{id}'), Middlewares([CheckAuthMiddleware::class])]
    public function show(mixed $id, OrderService $service): Order
    {
        return $service->show($id);
    }

    /**
     * 取消订单
     * @param mixed $id
     * @param OrderService $service
     * @return void
     */
    #[PutMapping('orders/{id}/cancel'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function updateCancel(mixed $id, OrderService $service): void
    {
        $service->updateCancel($id);
    }

    /**
     * 确认还车
     * @param mixed $id
     * @param OrderService $service
     * @return void
     */
    #[PutMapping('orders/{id}/return'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function updateReturn(mixed $id, OrderService $service): void
    {
        $service->updateReturn($id);
    }
}
